/*

 */

/*
 * GuardarCorreo2.java
 *
 * Created on 8/06/2009, 06:22:42 PM
 */

package presentacion;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.html.HTMLEditorKit;

/**
 *
 * @author Jaguar
 */
public class GuardarCorreo extends javax.swing.JDialog {

    /** Creates new form GuardarCorreo2 */
    public GuardarCorreo(IGListaDeCorreos parent, boolean modal, boolean isBD) {
        super(parent, modal);
        parent2 = parent;
        columnasAñadidas = new Properties();
        initComponents();
        if(!isBD){
            ingresarWhere.setEnabled(false);
        }
        where="";
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        asunto = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        mensaje = new javax.swing.JEditorPane();
        cargar = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        columnas = new javax.swing.JList();
        añadir = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        adjuntos = new javax.swing.JList();
        insertar = new javax.swing.JButton();
        examinar = new javax.swing.JButton();
        quitar = new javax.swing.JButton();
        eliminar = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        aceptar = new javax.swing.JButton();
        cancelar = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        para = new javax.swing.JTextField();
        ingresar = new javax.swing.JButton();
        ingresarWhere = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(690, 500));

        jLabel1.setFont(new java.awt.Font("Tahoma", 3, 18));
        jLabel1.setText("Mensaje a enviar");

        jLabel2.setText("Asunto:");

        jLabel3.setText("Mensaje:");

        mensaje.setContentType("text/html");
        jScrollPane1.setViewportView(mensaje);

        cargar.setText("Cargar HTML...");
        cargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cargarActionPerformed(evt);
            }
        });

        jLabel4.setText("Adjuntar:");

        columnas.setModel(columnasModel);
        jScrollPane2.setViewportView(columnas);

        añadir.setText("Añadir a la lista...");
        añadir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                añadirActionPerformed(evt);
            }
        });

        adjuntos.setModel(archivosModel);
        jScrollPane3.setViewportView(adjuntos);

        insertar.setText("Insertar en el Mensaje");
        insertar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insertarActionPerformed(evt);
            }
        });

        examinar.setText("Examinar...");
        examinar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                examinarActionPerformed(evt);
            }
        });

        quitar.setText("Quitar");
        quitar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitarActionPerformed(evt);
            }
        });

        eliminar.setText("Eliminar de la lista");
        eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminarActionPerformed(evt);
            }
        });

        jLabel5.setText("Etiquetas:");

        aceptar.setText("Aceptar");
        aceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aceptarActionPerformed(evt);
            }
        });

        cancelar.setText("Cancelar");
        cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarActionPerformed(evt);
            }
        });

        jLabel6.setText("Para:");

        para.setEditable(false);

        ingresar.setText("Ingresar");
        ingresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ingresarActionPerformed(evt);
            }
        });

        ingresarWhere.setText("Ingresar Condición");
        ingresarWhere.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ingresarWhereActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(166, 166, 166)
                        .addComponent(jLabel1))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel6))
                                .addGap(14, 14, 14)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(quitar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(examinar)))
                                    .addComponent(asunto, javax.swing.GroupLayout.DEFAULT_SIZE, 391, Short.MAX_VALUE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(para, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(ingresar))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(60, 60, 60)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(cargar)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 120, Short.MAX_VALUE)
                                        .addComponent(aceptar)
                                        .addGap(18, 18, 18)
                                        .addComponent(cancelar))
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 391, Short.MAX_VALUE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(ingresarWhere, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(eliminar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(insertar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(añadir, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(para, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(ingresar)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(asunto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(examinar)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(quitar)
                            .addComponent(jLabel3)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cargar)
                            .addComponent(cancelar)
                            .addComponent(aceptar)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(añadir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(insertar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(eliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ingresarWhere)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cargarActionPerformed
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Mensaje de los correos a enviar");
        int returnVal = chooser.showOpenDialog(this);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                //                mensaje.setPage(chooser.getSelectedFile().getAbsolutePath());
                mensaje.setPage(chooser.getSelectedFile().toURI().toURL());
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this,
                        "No se pudo cargar el mensaje, por favor verifique que\n" +
                        "el achivo no está siendo usado por otro programa.",
                        "Error de lectura",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
}//GEN-LAST:event_cargarActionPerformed

    private void añadirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_añadirActionPerformed
        SeleccionarColumnas dialog = new SeleccionarColumnas(this, true);
        dialog.setSeleccionandoRemitente(false);
        dialog.setVisible(true);
}//GEN-LAST:event_añadirActionPerformed

    private void insertarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertarActionPerformed
        int cursor = mensaje.getCaretPosition();
        try {
            mensaje.getDocument().insertString(cursor, (String) columnas.getSelectedValue(), null);
        } catch (BadLocationException ex) {
            //No insertar la etiqueta
        }
}//GEN-LAST:event_insertarActionPerformed

    private void examinarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_examinarActionPerformed
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Adjunto");
        int returnVal = chooser.showOpenDialog(this);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            archivosModel.addElement(chooser.getSelectedFile().getAbsolutePath());
        }
}//GEN-LAST:event_examinarActionPerformed

    private void quitarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitarActionPerformed
        Object seleccionado = adjuntos.getSelectedValue();
        archivosModel.removeElement( seleccionado );
}//GEN-LAST:event_quitarActionPerformed

    private void eliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarActionPerformed
        Object seleccionado = columnas.getSelectedValue();
        columnasModel.removeElement(seleccionado);
}//GEN-LAST:event_eliminarActionPerformed

    private void aceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aceptarActionPerformed
        int cantidadDeAdjuntos = adjuntos.getModel().getSize();
        String[] archivos = new String[ cantidadDeAdjuntos ];

        for (int i = 0; i < archivos.length; i++) {
            archivos[i]=(String) adjuntos.getModel().getElementAt(i);
        }

        //        parent2.guardarLista(columnasAñadidas, asunto.getText(), mensaje.getText(), archivos);
        HTMLEditorKit editor = new HTMLEditorKit();
        String mensajeEnHTML = new String();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            editor.write(os, mensaje.getDocument(), 0, mensaje.getDocument().getLength());
            mensajeEnHTML = os.toString();
            //            System.out.println("HTML:\n\n"+mensajeEnHTML);
            parent2.guardarLista(columnasAñadidas, asunto.getText(), mensajeEnHTML, archivos);
        } catch (IOException ex) {
            //nunca se lanzará
        } catch (BadLocationException ex) {
            //nunca se lanzará
        }

        this.dispose();
}//GEN-LAST:event_aceptarActionPerformed

    private void cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarActionPerformed
        this.dispose();
}//GEN-LAST:event_cancelarActionPerformed

    private void ingresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ingresarActionPerformed
        SeleccionarColumnas dialog = new SeleccionarColumnas(this, true);
        dialog.setSeleccionandoRemitente(true);
        dialog.setVisible(true);
}//GEN-LAST:event_ingresarActionPerformed

    private void ingresarWhereActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ingresarWhereActionPerformed
        IngresarWhere dialog = new IngresarWhere(this, true,where);
        dialog.setVisible(true);
}//GEN-LAST:event_ingresarWhereActionPerformed

    protected void añadirColumna(String id, String etiqueta){
        columnasAñadidas.setProperty(etiqueta, id);
        columnasModel.addElement(etiqueta);
    }

    protected void setPara(String id, String etiqueta){
        etiqueta="#destinatariosTO#";
        columnasAñadidas.setProperty(etiqueta, id);
        para.setText(id);
    }

    protected void setWhere(String cond){
        String etiqueta="#WHERE#";
        columnasAñadidas.setProperty(etiqueta, cond);
        where = cond;
    }

    public IGListaDeCorreos getParent2() {
        return parent2;
    }

    //Variables de clase
    private IGListaDeCorreos parent2;
    private Properties columnasAñadidas;
    private boolean configurarBD;
    private String where;

    private DefaultListModel archivosModel = new DefaultListModel();
    private DefaultListModel columnasModel = new DefaultListModel();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aceptar;
    private javax.swing.JList adjuntos;
    private javax.swing.JTextField asunto;
    private javax.swing.JButton añadir;
    private javax.swing.JButton cancelar;
    private javax.swing.JButton cargar;
    private javax.swing.JList columnas;
    private javax.swing.JButton eliminar;
    private javax.swing.JButton examinar;
    private javax.swing.JButton ingresar;
    private javax.swing.JButton ingresarWhere;
    private javax.swing.JButton insertar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JEditorPane mensaje;
    private javax.swing.JTextField para;
    private javax.swing.JButton quitar;
    // End of variables declaration//GEN-END:variables

}
